/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cxhd.lis.ctrl;

import akka.actor.ActorRef;
import com.cxhd.lis.DataCache;
import com.cxhd.lis.ctrl.actor.ActorManager;
import com.cxhd.lis.model.newmodel.LisPatient;
import com.cxhd.lis.model.newmodel.LisSamplecollection;
import com.cxhd.lis.model.newmodel.LisSqd;
import com.cxhd.lis.model.newmodel.lisTestResult;
import com.cxhd.lis.util.QrCodeRender;
import com.google.gson.Gson;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.kit.JsonKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author 52461
 */
@ControllerBind(controllerKey = "/view/sample")
public class SampleCtrl extends Controller {
    
    private LisSamplecollection info = new LisSamplecollection();
    private LisPatient patient = new LisPatient();
    private Gson gson = new Gson();
    private Map<String, String> config = DataCache.getIntance().getConfig();
    
    public void index() {
        render("/view/sample.jsp");
    }
    
    public void getUserList() {
        String sql = "select a.UUID, a.SEND_CENTER_STATUS,c.hospital_name,a.RETURN_RESULT_FLAG, a.PK,b.PK as brpk,a.BARCODE,b.PATIENTID,b.PATIENTNAME,b.SEX,b.AGE,b.PHONENO,a.SECTION,a.STAYHOSPITALMODE,a.EXAMINAIM,a.EXAMINAIMCODE,a.SAMPLE,a.BED_NO,a.CYSJ,a.YBLB,b.BIRTHDAY";
        String param = " from lis_samplecollection a left join lis_patient b on a.INFOPK=b.PK left join lis_hospital c on c.hospital_code=a.HOSPITAL_CODE WHERE a.YBLB = '1' order by a.CYSJ desc ";
        String sea = getPara("searchContent");
        if (sea != null && sea != "") {
            param = " from lis_samplecollection a left join lis_patient b on a.INFOPK=b.PK WHERE a.YBLB = '1' and  b.PATIENTID LIKE '%" + getPara("searchContent") + "%' or b.PATIENTNAME LIKE '%" + getPara("searchContent") + "%' order by a.CYSJ desc";
        }
        Page<Record> userPage = Db.use("mysql").paginate(getParaToInt("currentPage", 1),
                getParaToInt("pageSize", 10), sql, param);
        renderJson(userPage);
    }
    
    public void getKsmc() {
        renderJson(Db.use("mysql").find("select ksmc FROM lis_ksdm"));
    }
    
    public void getSampleType() {
        renderJson(Db.use("mysql").find("select SAMPLEDESCRIBE FROM lis_sampletype"));
    }
    
    public void getXMZT() {
        renderJson(Db.use("mysql").find("select ITEMNAME from lis_charginitem ORDER BY ITEMNAME"));
    }
    
    @Before(Tx.class)
    public void addSample() {
        String req = getSessionAttr("name");
        try {
            String pk = patient.insertInfo(this);
            String samplepk = info.insertSample(this, req, pk);
            if (config.get("autoSendLisCenter") != null && config.get("autoSendLisCenter").equalsIgnoreCase("true")) {
                ActorManager.getIntance().getActor("SendInfoToCenterActor").tell(samplepk, ActorRef.noSender());
            }
            renderText("true");
        } catch (Exception ex) {
            ex.printStackTrace();
            renderText("false");
        }
    }
    
    @Before(Tx.class)
    public void updateSample() {
        try {
            patient.updateInfo(this);
            info.updateInfo(this);
            renderText("true");
        } catch (Exception ex) {
            ex.printStackTrace();
            renderText("false");
        }
    }
    
    @Before(Tx.class)
    public void deleteInfo() {
        try {
            info.deleteInfo(this);
//            patient.deleteInfo(this);
            renderText("true");
        } catch (Exception ex) {
            ex.printStackTrace();
            renderText("false");
        }
    }
    
    public void testqr() {
        render(new QrCodeRender(getPara("data")));
    }
    
    public void sendToCenter() {
        ActorManager.getIntance().getActor("SendInfoToCenterActor").tell(getPara("pk"), ActorRef.noSender());
        renderText("true");
    }
    
    public void printRpt() {
        
        String uuid = getPara("uuid");
        String print = getPara("print");
        setAttr("uuid", uuid);
        setAttr("print", print);
//        System.out.println("select * from lis_samplecollection where UUID='" + uuid + "'");
//        setAttr("sampleinfo", Db.findFirst("select * from lis_samplecollection where UUID='" + uuid + "'").toJson());
//        System.out.println("select * from lis_result where uuid='" + uuid + "'");
//        setAttr("result", JsonKit.toJson(Db.find("select * from lis_result where uuid='" + uuid + "'")).replace("null", "''"));
        render("/view/printReportBarcode.jsp");
    }
    
    public void getInfoResult() {
        try {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("sampleinfo", Db.findFirst("select * from lis_samplecollection a left join lis_patient b on a.INFOPK=b.PK where a.UUID='" + getPara("uuid") + "'"));
            
            map.put("result", Db.find("select * from lis_result where uuid='" + getPara("uuid") + "'"));
            //System.out.println(JsonKit.toJson(map));
            renderJson(map);
        } catch (Exception e) {
            e.printStackTrace();
            renderText("false");
        }
        
    }
    
}
